﻿
Partial Class store_sermons
    Inherits CSearchPage

#Region "Data"
    Private m_products As CProductList
    Public ReadOnly Property [Products]() As CProductList
        Get
            If IsNothing(m_products) Then
                m_products = CProduct.Cache.Active.Sermons

                If SpeakerId > 0 Then m_products = m_products.GetBySpeaker1or2Id(SpeakerId)
                If LanguageId > 0 Then m_products = m_products.GetByLanguageId(LanguageId)
                m_products = m_products.Search(Search)

                If String.IsNullOrEmpty(Search) Then m_products = m_products.SortByCreatedDescName 'otherwise sort by relevance
            End If
            Return m_products
        End Get
    End Property
    Protected Overrides ReadOnly Property TypeId As SchemaMikeConnellMinistries.EProductType
        Get
            Return EProductType.Sermon
        End Get
    End Property
#End Region

#Region "Menu"
    Protected Overrides ReadOnly Property TopPanel As System.Web.UI.WebControls.PlaceHolder
        Get
            Return plhTopLinks
        End Get
    End Property
    Protected Overrides ReadOnly Property SubPanel As System.Web.UI.WebControls.PlaceHolder
        Get
            Return plhSubLinks
        End Get
    End Property
#End Region

#Region "Event Handlers - Page"
    Protected Overrides Sub PageInit()
        With Request.FilePath.ToLower
            If .Contains("/store/") OrElse .Contains("/mobile/") OrElse .Contains("all_downloads.aspx") OrElse .Contains("sunday_sermons.aspx") Then Response.RedirectPermanent(CSitemap.Sermons(Search, SpeakerId, LanguageId)) 'Renamed page
        End With

        lnkDonate.NavigateUrl = CSitemap.External_Donations

        'Tweak MetaDesc
        If SpeakerId > 0 Then
            Dim s As CSpeaker = CSpeaker.Cache.GetById(SpeakerId)
            If IsNothing(s) OrElse s.ActiveCount = 0 Then Response.RedirectPermanent(CSitemap.Sermons)


            lblSpeaker1.Text = s.SpeakerName
            lblSpeaker2.Text = s.SpeakerName
            imgSpeaker1.AlternateText = s.SpeakerName
            imgSpeaker2.AlternateText = s.SpeakerName
            imgSpeaker1.ToolTip = s.SpeakerAbout
            imgSpeaker2.ToolTip = s.SpeakerAbout
            imgSpeaker1.ImageUrl = s.RandomPhotoUrl(CSitemap.SpeakerUploads & s.SpeakerImage, 1)
            imgSpeaker2.ImageUrl = s.RandomPhotoUrl(CSitemap.SpeakerUploads & s.SpeakerImage, 2)
            If imgSpeaker1.ImageUrl.EndsWith("/") Then
                imgSpeaker1.Visible = False
                imgSpeaker2.Visible = False
                lblSpeaker1.Visible = False
                lblSpeaker2.Visible = False
            Else
                lnkFbImg.Attributes("href") = "http://mikeconnellministries.com/uploads/speakers/" & s.SpeakerImage

                If Not String.IsNullOrEmpty(s.SpeakerWebsite) Then
                    lnkSpeaker1.Text = CUtilities.Truncate(s.SpeakerWebsite)
                    lnkSpeaker2.Text = CUtilities.Truncate(s.SpeakerWebsite)
                    lnkSpeaker1.NavigateUrl = "http://" & s.SpeakerWebsite
                    lnkSpeaker2.NavigateUrl = "http://" & s.SpeakerWebsite
                    lnkSpeaker1.ToolTip = String.Concat(s.SpeakerName, "'s Website")
                    lnkSpeaker2.ToolTip = String.Concat(s.SpeakerName, "'s Website")

                    If Not String.IsNullOrEmpty(s.SpeakerContactPhone) Then
                        lnkSpeaker2.Text = "Products on Amazon&raquo;"
                        lnkSpeaker2.NavigateUrl = s.SpeakerContactPhone
                    End If
                End If
            End If
        Else
            imgSpeaker1.Visible = False
            imgSpeaker2.Visible = False

        End If


        'Dropdowns
        For Each i As CSpeaker In CSpeaker.Cache.HavingProducts
            CDropdown.Add(ddSpeaker, i.NameAndCountActive, i.SpeakerId)
        Next
        CDropdown.BlankItem(ddSpeaker, "-- Any Speaker --", 0)
        CDropdown.SetValue(ddSpeaker, SpeakerId)
        If SpeakerId > 0 AndAlso ddSpeaker.SelectedIndex <= 0 Then
            Dim s As CSpeaker = CSpeaker.Cache.GetById(SpeakerId)
            CDropdown.Add(ddSpeaker, s.SpeakerName, SpeakerId)
            CDropdown.SetValue(ddSpeaker, SpeakerId)
        End If

        For Each i As CLanguage In CLanguage.Cache
            If i.ProductsCount = 0 Then Continue For
            CDropdown.Add(ddLanguage, i.NativeAndCountActive, i.LanguageId)
        Next
        CDropdown.BlankItem(ddLanguage, "-- Any Language --", 0)
        CDropdown.SetValue(ddLanguage, LanguageId)


        'Search state (from querystring)
        txtSearch.Text = Me.Search


        'Metadata adjustments
        If Search.Length > 0 Then
            Dim s As String = String.Concat("""", Search, """ ", Title)
            Title = Title.Replace("Sermons", s)
            MetaDescription = String.Concat(s, ": ", MetaDescription)
        End If

        If LanguageId > 0 AndAlso LanguageId <> ELanguage.English Then
            Dim lang As String = CLanguage.Cache.GetById(LanguageId).NativeAndName
            Title = String.Concat(lang, " ", Title)
            MetaDescription = String.Concat(MetaDescription, " (", lang, ")")
        End If

        If SpeakerId > 0 AndAlso SpeakerId <> ESpeaker.MikeConnell Then
            Dim speak As String = CSpeaker.Cache.GetById(SpeakerId).SpeakerName
            'Title = String.Concat(speak, ": ", Title)
            Title = Title.Replace("Mike Connell", speak)
            MetaDescription = String.Concat(speak, ": ", MetaDescription)
        End If


    End Sub
    Protected Overrides Sub PagePreRender()
        'Paging and display
        Dim list As CProductList = Products
        For Each i As CProduct In ctrlPaging.display(list)
            UCSermon2(plh).Display(i)
        Next


        'Emphasise active filters
        If CDropdown.GetByte(ddSpeaker) > 0 Then ddSpeaker.ForeColor = Drawing.Color.DarkBlue
        If CDropdown.GetByte(ddLanguage) > 0 Then ddLanguage.ForeColor = Drawing.Color.DarkBlue
        If txtSearch.Text.Length > 0 Then txtSearch.ForeColor = Drawing.Color.DarkBlue


        'Show Languages (LHS - small set)
        For Each i As CLanguage In CLanguage.Cache
            If i.IsEnglish Then Continue For
            SideLink(colLeft, i)
        Next
        AddBr(colLeft)

        'Show Speakers (LHS - large set)
        Dim limit As Integer = 32 + 36
        If Products.Count < 50 Then limit = 1.4 * Products.Count - 5
        Dim lnk As HyperLink = SideLink(colLeft, CUtilities.NameAndCount("All Sermons", CProduct.Cache.Active.Sermons), CSitemap.Sermons, "All Sermons (any speaker)")
        If SpeakerId <= 0 AndAlso LanguageId <= 0 Then MarkSelected(lnk)
        For Each i As CSpeaker In CSpeaker.Cache.HavingProducts.SortByActiveCountDescName()
            If limit <= 0 Then Exit For
            limit -= 1
            SideLink(colLeft, i)
        Next


        'Show Tags (RHS)
        Dim tags As List(Of CProductList.CTagFreq) = CProduct.Cache.Active.Tags
        If LanguageId = ELanguage.Chinese Then
            tags = CProduct.Cache.Active.Chinese.AltTags
        End If
        limit = 36 + 36
        If Products.Count < 50 Then limit = 1.4 * Products.Count
        For Each i As CProductList.CTagFreq In tags
            If limit = 0 Then Exit For
            limit -= 1
            SideLink(colRight, i)
        Next

        'If list.Count > 40 Then
        '    AddBr(colLeft)
        '    AddBr(colRight)
        '    UCAdsenseSide(colLeft)
        '    UCAdsenseSide(colRight)
        'End If
    End Sub
#End Region

#Region "Event Handlers - Form"
    Protected Sub btnSearch_Click(sender As Object, e As System.EventArgs) Handles btnSearch.Click
        Response.Redirect(CSitemap.Sermons(txtSearch.Text, CDropdown.GetInt(ddSpeaker), CDropdown.GetInt(ddLanguage)))
    End Sub
    Protected Sub ddLanguage_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles ddLanguage.SelectedIndexChanged 'Language resets speaker
        Response.Redirect(CSitemap.Sermons(txtSearch.Text, 0, CDropdown.GetInt(ddLanguage)))
    End Sub
    Protected Sub ddSpeaker_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles ddSpeaker.SelectedIndexChanged 'speaker resets language
        Response.Redirect(CSitemap.Sermons(txtSearch.Text, CDropdown.GetInt(ddSpeaker), 0))
    End Sub
#End Region

#Region "User Controls"
    Private Shared Function UCSermon2(ByVal target As Control) As store_UCSermon2
        Dim ctrl As Control = target.Page.LoadControl(CSitemap.Store_UCSermon2)
        target.Controls.Add(ctrl)
        Return CType(ctrl, store_UCSermon2)
    End Function
    Private Shared Function UCAdsenseSide(ByVal target As Control) As usercontrols_UCAdsenseSide
        Dim ctrl As Control = target.Page.LoadControl("~/usercontrols/UCAdsenseSide.ascx")
        target.Controls.Add(ctrl)
        Return CType(ctrl, usercontrols_UCAdsenseSide)
    End Function
#End Region

End Class

